//
//  CDSetAccountViewController.m
//  CuriosityDaily3
//
//  Created by donghao on 16/9/25.
//  Copyright © 2016年 theBestTeam. All rights reserved.
//

#import "CDSetAccountViewController.h"

#import "CDSettingFirstCell.h"
#import "CDSettingThirdCell.h"

#import "UIImage+CDFillImage.h"
#import "CDSetOneDetailController.h"


@interface CDSetAccountViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic,weak) UITableView *tableView;
/** 组头label的内容*/
@property(nonatomic,strong) NSArray *sectionTitles;
/** cell 的内容*/
@property(nonatomic,strong) NSArray *datas;
/** 社交账号图片*/
@property(nonatomic,strong) NSArray *shareImageArray;
@property(nonatomic,strong) NSArray *shareSelImageArray;

#warning 登陆后从本地读取
/** 头像*/
@property(nonatomic,strong) UIImage *iconImage;
/** 姓名*/
@property(nonatomic,copy) NSString *name;
/** 简介*/
@property(nonatomic,copy) NSString *desc;
/** 邮箱*/
@property(nonatomic,copy) NSString *email;
/** 地址*/
@property(nonatomic,copy) NSString *address;
/** 手机*/
@property(nonatomic,copy) NSString *iphone;
/** 密码*/
@property(nonatomic,copy) NSString *paw;


@property(nonatomic,strong) NSMutableDictionary *userData;


@end

// 第二种
static NSString *twoLabelCellid = @"twoLabelCellid";
// 第一种cell
static NSString *firstCellid = @"firstCellid";


@implementation CDSetAccountViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
#warning 背景是截图
    self.view.backgroundColor = [UIColor darkGrayColor];
    [self loadData];
    [self setAccountData];
    [self setThirdAccountIcon];
    [self setupUI];
    
    
}

/**
 *  设置界面
 */
- (void)setupUI {
    self.automaticallyAdjustsScrollViewInsets = NO;
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,64, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    tableView.backgroundColor = [UIColor clearColor];
    
    [tableView registerNib:[UINib nibWithNibName:@"CDSettingFirstCell" bundle:nil] forCellReuseIdentifier:firstCellid];
    [tableView registerClass:[CDSettingThirdCell class] forCellReuseIdentifier:twoLabelCellid];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    [self setHeaderView];
}
#pragma mark - 设置头视图
- (void)setHeaderView {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    
#warning 从本地获取数据
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 54, 54)];
    imageView.image = self.iconImage;
    imageView.contentMode = UIViewContentModeScaleToFill;
    imageView.layer.cornerRadius = imageView.frame.size.width * 0.5;
    imageView.layer.masksToBounds = YES;
    
    UILabel *label = [UILabel labelWithText:@"编辑头像" andTextColor:[UIColor whiteColor] andFontSize:13];
    label.textAlignment = NSTextAlignmentCenter;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CDScreen.size.width, 120)];
    [headerView addSubview:imageView];
    imageView.center = CGPointMake(headerView.center.x, headerView.center.y - 15);
    [headerView addSubview:label];
    label.center = CGPointMake(headerView.center.x, headerView.center.y + 25);
    label.bounds = CGRectMake(0, 0, 54, 10);
    button.center = headerView.center;
    button.bounds = CGRectMake(0, 0, 56, 84);
    [headerView addSubview:button];
    [button addTarget:self action:@selector(setAccountIcon) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableHeaderView = headerView;
}

#pragma mark - 编辑头像
- (void)setAccountIcon {
    NSLog(@"编辑头像");
}

#pragma mark - table view data source 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.datas[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    NSString *leftTitle = self.datas[indexPath.section][indexPath.row];
    if (indexPath.section != 2) {
        cell = [self firstCellWithIndexPath:indexPath leftTitle:leftTitle rightTitle:self.userData[leftTitle]];

    } else {
        cell = [self otherCellWithIndexPath:indexPath leftTitle:leftTitle img:self.shareImageArray[indexPath.row] selectedImg:self.shareSelImageArray[indexPath.row]];

    }
    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *label = [UILabel labelWithText:self.sectionTitles[section] andTextColor:[UIColor whiteColor] andFontSize:13];
    label.backgroundColor = [UIColor clearColor];
    return label;
}
#pragma mark - 各种cell
- (CDSettingFirstCell *)firstCellWithIndexPath:(NSIndexPath *)indexPath leftTitle:(NSString *)leftTitle  rightTitle:(NSString *)rightTitle{
    CDSettingFirstCell *cell = [self.tableView dequeueReusableCellWithIdentifier:firstCellid forIndexPath:indexPath];
    [cell setLeftTitle:leftTitle arrow:YES];
    [cell setRightLabelText:rightTitle];
    return cell;
}

- (CDSettingThirdCell *)otherCellWithIndexPath:(NSIndexPath *)indexPath leftTitle:(NSString *)leftTitle img:(UIImage *)img selectedImg:(UIImage *)selectedImg{
    CDSettingThirdCell *cell = [self.tableView dequeueReusableCellWithIdentifier:twoLabelCellid forIndexPath:indexPath];
    [cell setLeftButtonWIthTitle:leftTitle];
    [cell setLeftButtonWithImage:img selectedImaeg:selectedImg];
    // 设置cell 的状态
    [cell setSwitchViewState:CDSwitchStateNormal];
    return cell;
}

#pragma mark - table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0 || indexPath.section == 1) {
            NSString *leftTitle = self.datas[indexPath.section][indexPath.row];
        if (indexPath.row == 0) {
            CDSetOneDetailController *oneVC = [[CDSetOneDetailController alloc] init];
            oneVC.desc = self.userData[leftTitle];
            [oneVC setNavBarWithTitle:leftTitle];
            __weak typeof(self) weakSelf = self;
            oneVC.detailBlock = ^(NSString *desc) {
                // 根据key改值
                weakSelf.userData[leftTitle] = desc;
                [weakSelf.tableView reloadData];
            };
            [self.navigationController pushViewController:oneVC animated:YES];
        }
        
    }
    
}


#pragma mark - loadData
- (void)loadData {
    
    NSArray *sectionTitles = @[@"    个人信息",
                               @"    联系方式",
                               @"    绑定社交账号",
                               @""];
    self.sectionTitles = sectionTitles;
    
    NSArray *oneSections = @[@"姓名", @"简介"];
    NSArray *twoSections = @[@"邮箱", @"地址", @"手机"];
    NSArray *threeSections = @[@"微信", @"新浪微博", @"QQ"];
    NSArray *fourSections = @[@"修改密码"];
    NSArray *datas = @[oneSections, twoSections, threeSections, fourSections];
    self.datas = datas;
}
/**
 从本地读取数据，
 */
- (void)setAccountData {
    self.iconImage = [UIImage imageNamed:@"风吹雪"];
    self.name = @"风吹雪";
    self.desc = @"啦啦啦啦啦";
    self.email = @"123445634@qq.con";
    self.address = @"花果山水帘洞";
    self.iphone = @"923432443";
    self.paw = @"dghaodn";
    NSDictionary *dict = @{
                           @"头像" : self.iconImage,
                           @"姓名": self.name,
                           @"简介": self.desc,
                           @"邮箱" : self.email,
                           @"地址" : self.address,
                           @"手机" : self.iphone,
                           };
    self.userData = [NSMutableDictionary dictionaryWithDictionary:dict];
}

- (void)setThirdAccountIcon {
    UIImage *imageQQ = [UIImage imageNamed:@"shareQQ"];
    UIImage *imageWechat = [UIImage imageNamed:@"shareWechatFriends"];
    UIImage *imageWeibo = [UIImage imageNamed:@"shareWeibo"];
    
    UIImage *selImageQQ = [imageQQ imageWithTintColor:[UIColor whiteColor]];
    UIImage *selImageWechat = [imageWechat imageWithTintColor:[UIColor whiteColor]];
    UIImage *selImageWeibo = [imageWeibo imageWithTintColor:[UIColor whiteColor]];
    
    
    NSArray *shareImageArray = @[imageWechat, imageWeibo, imageQQ];
    NSArray *shareSelImageArray = @[selImageWechat, selImageWeibo, selImageQQ];
    
    self.shareImageArray = shareImageArray;
    self.shareSelImageArray = shareSelImageArray;
}

@end
